package org.particlethink.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.particlethink.auth.DefaultUser;
import org.particlethink.context.Context;
import org.particlethink.context.DefaultContext;
import org.particlethink.mapper.MasterPlanCategoryMapper;
import org.particlethink.mapper.MasterPlanMapper;
import org.particlethink.mapper.ProjectMapper;
import org.particlethink.mapper.ProjectUserMapper;
import org.particlethink.model.*;
import org.particlethink.response.BaseResponse;
import org.particlethink.response.MasterPlanCategoryResponse;
import org.particlethink.response.OptionResponse;
import org.particlethink.response.ProjectResponse;
import org.particlethink.utils.ListUtilsKt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Tag(name = "项目")
@RestController
@RequestMapping("/project")
public class ProjectController {


    @Autowired
    public ProjectMapper projectMapper;
    @Autowired
    public MasterPlanCategoryMapper masterPlanCategoryMapper;
    @Autowired
    public MasterPlanMapper masterPlanMapper;
    @Autowired
    public ProjectUserMapper projectUserMapper;

    @Operation(summary = "项目列表")
    @GetMapping("/list")
    public BaseResponse<List<ProjectResponse>> list(
            @RequestParam(required = false)
            @Parameter(description = "项目名称")
            String projectName,
            @RequestParam(required = false)
            @Parameter(description = "管理层级名称")
            String manageLvName
//            @RequestParam
//            @Parameter(description = "管理层级ID")
//            Integer manageLvId
    ) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        DefaultUser defaultUser = (DefaultUser) authentication.getPrincipal();
        User user = defaultUser.getUser();

        List<Project> projects = projectMapper.list(user.getToken(), projectName, manageLvName, ((DefaultUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getProjectIds());
        return BaseResponse.success(projects.stream().map(project -> new ProjectResponse(project)).collect(Collectors.toList()));
    }


    @GetMapping("/{id}/master_plan_category")
    public BaseResponse<List<MasterPlanCategoryResponse>> MasterPlanCategories(@PathVariable("id") Integer id) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        DefaultUser defaultUser = (DefaultUser) authentication.getPrincipal();
        User user = defaultUser.getUser();

        List<MasterPlan> masterPlans = masterPlanMapper.selectByProjectId(id);
        Map<String, MasterPlan> map = ListUtilsKt.map(masterPlans, MasterPlan::getMasterPlanCategoryId);

        List<MasterPlanCategory> masterPlanCategories = masterPlanCategoryMapper.all(user.getToken());
        List<MasterPlanCategory> masterPlanCategories2 = masterPlanCategories.stream().filter(masterPlanCategory -> !map.containsKey(masterPlanCategory.getId())).collect(Collectors.toList());

        List<MasterPlanCategoryResponse> collect = masterPlanCategories2.stream().map(MasterPlanCategoryResponse::new).collect(Collectors.toList());
        return BaseResponse.success(collect);
    }


    @GetMapping("/{id}/master_plain")
    public BaseResponse<List<OptionResponse>> MasterPlans(@PathVariable("id") Integer id) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        DefaultUser defaultUser = (DefaultUser) authentication.getPrincipal();
        User user = defaultUser.getUser();

        Context ctx = DefaultContext.Builder.background().withValue("token", ((DefaultUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser().getToken());

        List<MasterPlan> masterPlans = masterPlanMapper.selectSubmittedByProjectId(id);

//        List<Integer> projectIDs = masterPlans.stream().map(MasterPlan::getProjectId).collect(Collectors.toList());
//        List<Project> projects = this.projectMapper.selectByIds(user.getToken(), projectIDs);
//        Map<Integer, Project> projectMap = ListUtilsKt.map(projects, Project::getId);
//
//
//        List<Integer> projectUserIds = masterPlans.stream().flatMap(plan -> Stream.of(plan.getCreatorId(), plan.getSubmitId())).distinct().collect(Collectors.toList());
//        List<ProjectUser> projectUsers = this.projectUserMapper.selectByIds(projectUserIds);
//        Map<Integer, ProjectUser> projectUserMap = ListUtilsKt.map(projectUsers, ProjectUser::getId);
//
//
//        List<String> masterPlanCategoryIds = masterPlans.stream().map(MasterPlan::getMasterPlanCategoryId).collect(Collectors.toList());
//        List<MasterPlanCategory> masterPlanCategories = this.masterPlanCategoryMapper.selectByIds(user.getToken(), masterPlanCategoryIds);
//        Map<String, MasterPlanCategory> masterPlanCategoryMap = ListUtilsKt.map(masterPlanCategories, MasterPlanCategory::getId);


        List<OptionResponse> results = masterPlans.stream().map(masterPlan -> {
            return new OptionResponse(masterPlan.getId(), masterPlan.getNo());
        }).collect(Collectors.toList());


        return BaseResponse.success(results);
    }

}
